/**
 * Filename: EndlessRecyclerOnScrollListener.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/4/18 15:09
 */
package com.github.postsanf.yinian.ynlist;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
public class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener implements OnListLoadNextPageListener {

	/**
	 * 当前RecyclerView类型
	 */
	protected LayoutManagerType layoutManagerType;

	/**
	 * 最后一个的位置
	 */
	private int[] lastPositions;

	/**
	 * 最后一个可见的item的位置
	 */
	private int lastVisibleItemPosition;

	/**
	 * 当前滑动的状态
	 */
	private int currentScrollState = 0;

	@Override
	public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
		super.onScrolled(recyclerView, dx, dy);

		RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

		if (layoutManagerType == null) {
			if (layoutManager instanceof LinearLayoutManager) {
				layoutManagerType = LayoutManagerType.LinearLayout;
			} else if (layoutManager instanceof GridLayoutManager) {
				layoutManagerType = LayoutManagerType.GridLayout;
			} else if (layoutManager instanceof StaggeredGridLayoutManager) {
				layoutManagerType = LayoutManagerType.StaggeredGridLayout;
			} else {
				throw new RuntimeException(
						"Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
			}
		}

		switch (layoutManagerType) {
			case LinearLayout:
				lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
				break;
			case GridLayout:
				lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
				break;
			case StaggeredGridLayout:
				StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
				if (lastPositions == null) {
					lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
				}
				staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
				lastVisibleItemPosition = findMax(lastPositions);
				break;
		}
	}

	@Override
	public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
		super.onScrollStateChanged(recyclerView, newState);
		currentScrollState = newState;
		RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
		int visibleItemCount = layoutManager.getChildCount();
		int totalItemCount = layoutManager.getItemCount();
		if ((visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItemPosition) >= totalItemCount - 1)) {
			onLoadNextPage(recyclerView);
		}
	}

	/**
	 * 取数组中最大值
	 *
	 * @param lastPositions
	 * @return
	 */
	private int findMax(int[] lastPositions) {
		int max = lastPositions[0];
		for (int value : lastPositions) {
			if (value > max) {
				max = value;
			}
		}

		return max;
	}

	@Override
	public void onLoadNextPage(final View view) {
	}

	public static enum LayoutManagerType {
		LinearLayout,
		StaggeredGridLayout,
		GridLayout
	}
}